<?php

namespace easydis\APSBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use easydis\APSBundle\Entity\UO;
use easydis\APSBundle\Entity\Heures;
use easydis\APSBundle\Entity\CommentaireGS;

use \DateTime;
use \DateInterval;

class DashboardController extends Controller
{
    
    public function prevJAction($exploit, $date, $mode)
    {
        $em = $this->getDoctrine()->getManager();

        $repository_exploit = $em->getRepository('easydisAPSBundle:Exploitation');
        $repository_gs = $em->getRepository('easydisAPSBundle:GroupeService');
        $repository_service = $em->getRepository('easydisAPSBundle:Service');

        $tabResult = array(array());
        $tabTotalExploit = array();
        $ligne = 0;
        $list_gs = $repository_gs->findBy(array('exploit' => $exploit, 'type'=>'1'));
        foreach($list_gs as $gs) {
            //Recuperation de la ligne Sous-total niveau groupe de service
            $tabResult[$ligne][0] = 'groupe_service';
            $tabResult[$ligne][1] = $gs->getLib();
            $resultsheures = $repository_gs->getSumHeures($gs, $date);
            if($resultsheures){
                $tabResult[$ligne][2] = number_format($resultsheures[0][1],1);
                $tabResult[$ligne][3] = number_format($resultsheures[0][2],1);
                $tabResult[$ligne][4] = number_format($resultsheures[0][1] - $resultsheures[0][2],1);
            }
            $resultsuo = $repository_gs->getSumUo($gs, $date);
            if($resultsuo){
                $tabResult[$ligne][5] = number_format($resultsuo[0][1], 0, ',', ' ');
                $tabResult[$ligne][6] = number_format($resultsuo[0][2], 0, ',', ' ');
                $tabResult[$ligne][7] = ($resultsuo[0][1] <> "" && $resultsuo[0][2] <> "") ? number_format($resultsuo[0][1] - $resultsuo[0][2], 0, ',', ' ') : "";
            }
            $prodObj = ($tabResult[$ligne][3] <> 0) ? number_format($resultsuo[0][2] / $resultsheures[0][2],1) : 'N/A';
            $prodPrev = ($tabResult[$ligne][2] <> 0) ? number_format($resultsuo[0][1] / $resultsheures[0][1],1) : 'N/A';

            $tabResult[$ligne][8] = $prodPrev;
            $tabResult[$ligne][9] = $prodObj;
            $tabResult[$ligne][10] = ($prodObj <> 'N/A' && $prodObj <> 0) ? number_format(-($resultsheures[0][1]-($resultsuo[0][1]/$prodObj)),0) : 'N/A';
            $tabResult[$ligne][11] = $gs;
            $ligne = $ligne + 1; // On change de ligne pour les services
            
            // Recuperation de tout les services integrés au groupe de services courant
            $list_service = $repository_service->getGroupeServices($gs);
            foreach($list_service as $service){
                $tabResult[$ligne][0] = 'service';
                $tabResult[$ligne][1] = $service->getLib();
                $resultsheures = $repository_service->getSumHeures($service, $date);
                if($resultsheures){
                    $tabResult[$ligne][2] = number_format($resultsheures[0][1], 2);
                    $tabResult[$ligne][3] = number_format($resultsheures[0][2], 2);
                    $tabResult[$ligne][4] = number_format($resultsheures[0][1] - $resultsheures[0][2],2);
                }
                $resultsuo = $repository_service->getSumUo($service, $date);
                if($resultsuo){
                    $tabResult[$ligne][5] = number_format($resultsuo[0][1], 0, ',', ' ');
                    $tabResult[$ligne][6] = number_format($resultsuo[0][2], 0, ',', ' ');
                    $tabResult[$ligne][7] = ($resultsuo[0][1] <> "" && $resultsuo[0][2] <> "") ? number_format($resultsuo[0][1] - $resultsuo[0][2], 0, ',', ' ') : "";
                }
                $prodObj = ($resultsheures[0][2] <> 0) ? number_format($resultsuo[0][2] / $resultsheures[0][2],1) : 'N/A';
                $prodPrev = ($resultsheures[0][1] <> 0) ? number_format($resultsuo[0][1] / $resultsheures[0][1],1) : 'N/A';

                $tabResult[$ligne][8] = $prodPrev;
                $tabResult[$ligne][9] = $prodObj;
                $tabResult[$ligne][10] = ($prodObj <> 'N/A' && $prodObj <> 0) ? number_format(-($resultsheures[0][1]-($resultsuo[0][1]/$prodObj)),0) : 'N/A';
                $ligne = $ligne + 1; // On change de ligne pour le service/groupe de servie suivant
            }
        } 
        $resultsheures = $repository_exploit->getSumHeures($exploit, $date);
        if($resultsheures) {
            $tabTotalExploit[0] = number_format($resultsheures[0][1],2);
            $tabTotalExploit[1] = number_format($resultsheures[0][2],2);
            $tabTotalExploit[2] = number_format($resultsheures[0][1] - $resultsheures[0][2],2);
        }
        $resultsuo = $repository_exploit->getSumUO($exploit, $date);
        if($resultsuo) {
            $tabTotalExploit[3] = number_format($resultsuo[0][1], 0, ',', ' ');
            $tabTotalExploit[4] = number_format($resultsuo[0][2], 0, ',', ' ');
            $tabTotalExploit[5] = number_format($resultsuo[0][1] - $resultsuo[0][2], 0, ',', ' ');
        }
        $prodObj = ($resultsheures[0][2] <> 0) ? number_format($resultsuo[0][2] / $resultsheures[0][2], 1) : 'N/A';
        $prodPrev = ($resultsheures[0][1] <> 0) ? number_format($resultsuo[0][1] / $resultsheures[0][1],1) : 'N/A';

        $tabTotalExploit[6] = $prodPrev;
        $tabTotalExploit[7] = $prodObj;
        $tabTotalExploit[8] = ($prodObj <> 'N/A') ? number_format(-($resultsheures[0][1]-($resultsuo[0][1]/$prodObj)),0) : 'N/A';
        
        return $this->render('easydisAPSBundle:Dashboard:prevJ.html.twig', array('tabResult' => $tabResult, 'dateC' => $date, 'tabTotalExploit' => $tabTotalExploit, 'exploit' => $exploit));
    }
    
    public function add_updateAction($date, $gs) 
    {
        $em = $this->getDoctrine()->getManager();
		
        $repository_gs = $em->getRepository('easydisAPSBundle:GroupeService');
        $repository_service = $em->getRepository('easydisAPSBundle:Service');
        $repository_section = $em->getRepository('easydisAPSBundle:Section');
        $repository_uo = $em->getRepository('easydisAPSBundle:UO');
        $repository_heures = $em->getRepository('easydisAPSBundle:Heures');
        $repository_comm_gs = $em->getRepository('easydisAPSBundle:CommentaireGS');
        
        $gs = $repository_gs->findOneBy(array('id' => $gs));
        // Gestion de la reception du formulaire
        $request = $this->getRequest();
        $test = 0;
        if($request->getMethod() == 'POST')
        {
            $i = 0;
            while($request->request->get('type_'.$i)) {                
                $valeur = $request->request->get('element_'.$i);
                $key = $request->request->get('key_'.$i);
                $type = $request->request->get('type_'.$i);
                switch ($type) {
                    case 'U':
                        $uo = $repository_uo->findOneBy(array('date' => $date, 'service' => $key));
                        if (!$uo) { // Protege d'une double entrée
                            $uo = new UO($date, $repository_service->findOneBy(array('id' => $key)));
                        }
                        if ($valeur == "0") {
                            $uo->setUoprev(0);
                        } else {
                            $uo->setUoprev(intval($valeur));
                        }
                        $em->persist($uo);
                        break;
                    case 'H':
                        $heures = $repository_heures->findOneBy(array('date' => $date, 'section' => $key));
                        if (!$heures) { // Protege d'une double entrée
                            $heures = new Heures($date, $repository_section->findOneBy(array('id' => $key)));
                        }
                        if ($valeur == "0") {
                            $heures->setHprev(0.0);
                        } else {
                            $heures->setHprev(floatval($valeur));
                        }
                        $em->persist($heures);
                        break;
                    case 'C':
                        $comm_gs = $repository_comm_gs->findOneBy(array('date' => $date, 'groupe_services' => $key));
                        $comm_gs = ($comm_gs) ? $comm_gs : new CommentaireGS($date, $gs);
                        $comm_gs->setCommentaire($valeur);
                        $em->persist($comm_gs);
                        break;
                }
                $i = $i + 1;
                $test = $i;
            }
            $em->flush();
        }
        
        // Generation du formulaire
        $tabResult = array(array());
        $ligne = 0;
        $tabResult[$ligne][0] = 'groupe_service';
        $tabResult[$ligne][1] = $gs->getLib();

        $ligne = $ligne +1; // On change de ligne pour les services

        // Recuperation de tout les services integrés au groupe de services courant
        $list_service = $repository_service->getGroupeServices($gs);
        foreach($list_service as $service){
            $tabResult[$ligne][0] = 'service';
            $tabResult[$ligne][1] = $service->getLib();
            $ligne = $ligne +1; // On change de ligne pour le service/groupe de servie suivant
            
            if($service->getHonly() == 0) {
                $uo = $repository_uo->findOneBy(array('date' => $date, 'service' => $service));
                $tabResult[$ligne][0] = 'uo';
                $tabResult[$ligne][1] = ($uo) ? $uo : new UO($date, $service);
                $tabResult[$ligne][2] = $service;

                $ligne = $ligne +1; // On change de ligne pour le service/groupe de servie suivant
            }
            $list_section = $repository_section->findBy(array('service' => $service));
            foreach($list_section as $section){
                $tabResult[$ligne][0] = 'heures';
                $heures = $repository_heures->findOneBy(array('date' => $date, 'section' => $section));
                $tabResult[$ligne][1] = ($heures) ? $heures : new Heures($date, $section);;
                $ligne = $ligne + 1; // On change de ligne pour le service/groupe de servie suivant
            }
            if($service->getHonly() == 0) {
                $tabResult[$ligne][0] = 'total';
                $total_uo = $repository_service->getSumUo($service, $date);
                $total_h = $repository_service->getSumHeures($service, $date);
                $tmp_uo_prev = ($total_uo[0][1]) ? $total_uo[0][1] : 0;
                $tmp_uo_obj = ($total_uo[0][2]) ? $total_uo[0][2] : 0;
                $tmp_h_prev = ($total_h[0][1]) ? $total_h[0][1] : 0;
                $tmp_h_obj = ($total_h[0][2]) ? $total_h[0][2] : 0;

                $tabResult[$ligne][1] = ($tmp_h_prev <> 0) ? number_format($tmp_uo_prev/$tmp_h_prev,1) : 'N/A';
                $tabResult[$ligne][2] = ($tmp_h_obj <> 0) ? number_format($tmp_uo_obj/$tmp_h_obj,1) : 'N/A';
                $ligne = $ligne + 1; // On change de ligne pour le service/groupe de servie suivant
            }
        }
        // Recuperation du commentaire
        $tmp = $repository_comm_gs->findOneBy(array('date' => $date, 'groupe_services' => $gs));
        $commentaire = ($tmp) ? $tmp->getCommentaire() : '';
        return $this->render('easydisAPSBundle:Dashboard:add_update.html.twig', array('dateC' => $date, 'tabResult' => $tabResult, 'gs' => $gs, 'commentaire' => $commentaire, 'test' => $test));
    }
    
    public function redirectAction($date, $gs, $mode)
    {
       $response = $this->forward('easydisAPSBundle:Homepage:index');
        
       $request = $this->getRequest();
       if($request->getMethod() == 'POST')
       {
          $date = ($request->request->get('date')) ? $request->request->get('date') : $date;
       }
       if($mode == 'prevJ'){
            $response = $this->forward('easydisAPSBundle:Dashboard:prevJ', array(
            'date'  => $date,
            'exploit' => $gs,
            'mode' => 'UTILISABLE',
            ));
        } 
        else if ($mode == 'add_update') {
            $response = $this->forward('easydisAPSBundle:Dashboard:add_update', array(
            'date'  => $date,
            'gs' => $gs,
            ));
         }
        return $response;
    }
    
    public function dashboard_gsAction($date, $gs, $option = "Heures") {
        $em = $this->getDoctrine()->getManager();
		
        $repository_gs = $em->getRepository('easydisAPSBundle:GroupeService');
        
        $gs = $repository_gs->findOneBy(array('id' => $gs));
        if(!$gs) //Erreur !!
            return $this->render('easydisAPSBundle:Dashboard:dashboard_gs.html.twig', array('dateC' => $date->format('Y-m-d')));
        
        $date = date_create_from_format ('d/m/Y', $date);
        $tabResult = array(array());
        $ligne = 0;
        $col = 0;
        $prev = 1;
        $obj = 1;
        $reel = 1;
        $radio = "Heures";
        // tab[0][0] -> date, tab[0][1] -> libellé serie, tab[0][2] -> valeur serie, tab[0][3] -> libellé serie 2, tab[0][4] -> date
        $interval = new DateInterval('P10D');
        $dateDebut = clone $date;
        $dateFin = clone $date;
        $dateFin = $dateFin->add($interval);
        $interval->invert = 1;
        $dateDebut->add($interval);
        $increment = new DateInterval('P1D');
        
        $request = $this->getRequest();
        if($request->getMethod() == 'POST')
        { 
          $prev = ($request->request->get('Prev')) ? 1 : 0;
          $obj = ($request->request->get('Obj')) ? 1 : 0;
          $reel = ($request->request->get('Reel')) ? 1 : 0;
          
          $radio = $request->request->get('type');
          $d_debut = $request->request->get('date_debut');
          $d_fin = $request->request->get('date_fin');
          $dateDebut = date_create_from_format ('d/m/Y', $d_debut);
          $dateFin = date_create_from_format ('d/m/Y', $d_fin);
        }
        
        $ligne = 0;
        $i = 1;
        $tabResult[$ligne][0] = 'Date';
        if($prev == 1) {
            $tabResult[$ligne][$i] = 'Prev';
            $i = $i + 1;
        }
        if($obj == 1) {
            $tabResult[$ligne][$i] = 'Obj';
            $i = $i + 1;
        }
        if($reel == 1) {
            $tabResult[$ligne][$i] = 'Réel';
        }
        $ligne = 1;
        $dateDebut_increment = clone $dateDebut;
        while($dateDebut_increment <= $dateFin)
        {
            $tabUO = $repository_gs->getSumUo($gs, $dateDebut_increment->format('d/m/Y'));
            if($radio == "UO") {
                $tab = $tabUO;
            }
            else {
                $tab = $repository_gs->getSumHeures($gs, $dateDebut_increment->format('d/m/Y'));
            }
            if($tab && $tab[0][2] <> 0) {
                $i = 1;
                // Faire requete avec la dateDebut
                $tabResult[$ligne][0] = $dateDebut_increment->format('d M');
                if($prev == 1) {
                    if($radio == "Prod") {
                        $hprev = $tab[0][2];
                        $uoprev = ($tabUO) ? $tabUO[0][1] : "";
                        $tabResult[$ligne][$i] = ($hprev <> 0 && $uoprev <> "") ? number_format($uoprev/$hprev,2) : 0;
                    }
                    else
                        $tabResult[$ligne][$i] = ($radio == "Heures") ? number_format($tab[0][1], 2) : number_format($tab[0][1], 0, ',', '');
                    $i = $i + 1;
                }
                if($obj == 1) {
                    if($radio == "Prod") {
                        $hobj = number_format($tab[0][2],2);
                        $uoobj = ($tabUO) ? $tabUO[0][2] : "";
                        $tabResult[$ligne][$i] = ($hobj <> 0 && $uoobj <> "") ? number_format($uoobj/$hobj,2) : 0;
                    }
                    else
                        $tabResult[$ligne][$i] = ($radio == "Heures") ? number_format($tab[0][2], 2) : number_format($tab[0][2], 0, ',', '');
                    $i = $i + 1;
                }
                if($reel == 1) {
                    if($radio == "Prod") {
                        $hreel = number_format($tab[0][3],2);
                        $uoreel = ($tabUO) ? $tabUO[0][3] : "";
                        $tabResult[$ligne][$i] = ($hreel <> 0 && $uoreel <> "") ? number_format($uoreel/$hreel,2) : 0;
                    }
                    else
                        $tabResult[$ligne][$i] = ($radio == "Heures") ? number_format($tab[0][3], 2) : number_format($tab[0][3], 0, ',', '');
                }
                $ligne = $ligne + 1;
            }
            $dateDebut_increment->add($increment);
        }
        
        // Recuperation des totaux
        $tab = $repository_gs->getSumHeuresPeriode($gs, $dateDebut, $dateFin);
        $tabUO = $repository_gs->getSumUoPeriode($gs, $dateDebut, $dateFin);
        
        return $this->render('easydisAPSBundle:Dashboard:dashboard_gs.html.twig', array('dateC' => $date->format('d/m/Y'), 'dateD' => $dateDebut->format('d/m/Y'), 'dateF' => $dateFin->format('d/m/Y'), 
            'tabResult' => $tabResult, 'gs' => $gs, 'reel' => $reel, 'obj' => $obj, 'prev' => $prev, 'radio' => $radio, 'tabH' => $tab, 'tabUO' => $tabUO ));
    }
}